package com.hm.shopping.service;

import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.common.def.DataStatusDef;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.util.CommonUtil;
import com.hm.shopping.auto.su.BaseDomain;
import com.hm.shopping.domain.FavoritesDomain;
import com.hm.shopping.domain.QFavoritesDomain;
import com.hm.shopping.repository.FavoritesRepository;
import com.hm.shopping.su.BeanUtil;
import com.hm.shopping.vo.FavoritesVo;

/**
 * @author shishun.wang
 * @date 2018年2月28日 下午6:06:19
 * @version 1.0
 * @describe
 */
@Service
public class FavoritesServiceImpl implements FavoritesService {

	@Autowired
	private FavoritesRepository favoritesRepository;

	@Autowired
	private GoodsIntroductionService goodsIntroductionService;

	@Override
	public void addFavorites(String buyerId, String goodsId) {
		if (CommonUtil.isAnyEmpty(buyerId, goodsId)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		QFavoritesDomain qFavoritesDomain = QFavoritesDomain.favoritesDomain;
		if (favoritesRepository.exists(qFavoritesDomain.status.eq(DataStatusDef.ENABLE).and(qFavoritesDomain.goodsId.eq(goodsId)).and(qFavoritesDomain.buyerId.eq(buyerId)))) {
			return;
		}

		FavoritesDomain favoritesDomain = new FavoritesDomain();
		favoritesDomain.setBuyerId(buyerId);
		favoritesDomain.setGoodsId(goodsId);
		BaseDomain.initBaseInfo(favoritesDomain);
		favoritesRepository.save(favoritesDomain);
	}

	@Override
	public void delFavorites(String favoriteId) {
		if (CommonUtil.isEmpty(favoriteId)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		FavoritesDomain favoritesDomain = favoritesRepository.findOne(favoriteId);
		if (CommonUtil.isEmpty(favoritesDomain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}
		favoritesDomain.setStatus(DataStatusDef.DISABLE);
		favoritesRepository.save(favoritesDomain);
	}

	@Override
	@SuppressWarnings("unchecked")
	public PageInfo<FavoritesVo> queryFavorites(PageInfoParam pageParam, String buyerId) {
		if (CommonUtil.isEmpty(buyerId)) {
			return PageInfo.EMPTY;
		}

		QFavoritesDomain qFavoritesDomain = QFavoritesDomain.favoritesDomain;
		Pageable pageable = new QPageRequest(pageParam.getPage(), pageParam.getSize(), qFavoritesDomain.createTime.desc());
		Page<FavoritesDomain> page = favoritesRepository.findAll(qFavoritesDomain.status.eq(DataStatusDef.ENABLE).and(qFavoritesDomain.buyerId.eq(buyerId)), pageable);

		return new PageInfo<FavoritesVo>(page.getContent().parallelStream().map(item -> {
			FavoritesVo vo = new FavoritesVo();
			BeanUtil.copyProperties(item, vo);
			vo.setGoodsIntroductionVo(goodsIntroductionService.getGoodsIntroduction(item.getGoodsId()));
			return vo;
		}).collect(Collectors.toList()), page.getTotalPages(), page.getTotalElements());
	}

}
